$(function () {

    var menu_datagrid = $("#menu_datagrid");
    var menu_dialog = $("#menu_dialog");
    var menu_form = $("#menu_form");



    menu_datagrid.datagrid({
        fitColumns: true,
        fit: true,
        toolbar: '#menu_toolbar',
        url: '/menu/list.do',
        pagination: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '菜单名称', field: 'text', width: '100'},
            {title: '菜单编号', field: 'sn', width: '100'},
            {title: '菜单路径', field: 'url', width: '100'},
            {
                title: '是否有子菜单', field: 'child', width: '100', formatter: function (value, row, index) {
                    return row.children.length ? "<font color='green'>是</font>" : "<font color='red'>否</font>"
                }
            }
        ]],
        onLoadSuccess: function (data) {

        }
    })


    menu_dialog.dialog({
        width: 300,
        height: 250,
        buttons: '#menu_buttons',
        closed: true,
        onClose: function () {
            menu_form.form("clear")
        }
    })

    var methodObj = {
        //添加按钮
        add: function () {


            menu_dialog.dialog('setTitle', '新增菜单');

            menu_dialog.dialog("open");


        },
        //编辑按钮
        edit: function () {
            var data = menu_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }

            menu_form.form('load', data);

            menu_dialog.dialog("open");
            menu_dialog.dialog('setTitle', '编辑菜单');
        },
        //删除按钮
        remove: function () {
            var data = menu_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            $.messager.confirm("温馨提示", "你确定要删除该菜单吗？", function (r) {
                if (r) {
                    $.get("/menu/delete.do", {id: data.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "删除成功", "info", function () {
                                menu_datagrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    });
                }
            })
        },
        //回到上一级菜单按钮
        reload: function () {
            var nowMenu = $("#parentId").val();
            $.get("/menu/selectParent.do",{id:nowMenu},function(data){
                $("#parentId").val($(data.parent).prop("id"));
                menu_datagrid.datagrid("reload", {parentId:$(data.parent).prop("id")});
                return;
            })
        },
        //取消按钮
        cancel: function () {
            menu_dialog.dialog("close");
        },
        query: function () {
            var val = $("#keyword").textbox("getPanel", 0);
            menu_datagrid.datagrid('load', {
                keyword: val
            });
        },
        //添加保存按钮
        save: function () {
            menu_form.form('submit', {
                url: '/menu/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.cancel();
                            menu_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                }
            })
        },
        join: function () {
            var data = menu_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
             $("#parentId").val($(data).prop("id"));

            menu_datagrid.datagrid("reload", {parentId: data.id});
        }

    }


    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    })

})

